/**
 * Copyright 2011 Clemens Lombriser <clemens@lom.ch>
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * 
 */

package ch.lom.clemens.android.bibliography.data;

import android.content.Context;

public class MendeleyConnector {

	private static JSONWebConnector m_webcon;
	private static DBConnector      m_dbcon;

	public MendeleyConnector(Context ctx) {
		m_webcon = new JSONWebConnector();
		m_dbcon  = new DBConnector(ctx);
		m_dbcon.open();
	}

	// ///////////////////////////////////////////////////////////////////////////
	// Access data

	public MCollection [] getCollections() {
		
		// first try local cache
		MCollection [] collections = null; 
		
		collections = m_dbcon.getCollections(this);
		
		// local cache failed - get it online
		if (collections == null || collections.length == 0) {
			collections = m_webcon.getCollections();
		
			// update local cache
			if (collections != null) {
				m_dbcon.updateCollections(collections);
			}
		}
		
		return collections;
	}

	public String[] getCollectionDocIDs(MCollection col) {
		
		String [] docids = null;

		docids = m_dbcon.getCollection(col);
		
		// local cache failed - get it online
		if (docids == null || docids.length == 0) {
			docids = m_webcon.getCollectionDocIDs(col);
		
			// update local cache
			if (docids != null) {
				m_dbcon.updateCollectionDocs(col, docids);
			}
		}
		
		return docids;
	}
	
	public MCollection[] getGroups(MendeleyConnector con) {
		// first try local cache
		MGroup [] groups = m_dbcon.getGroups(this);
		
		// local cache failed - get it online
		if (groups == null || groups.length == 0) {
			groups = m_webcon.getGroups(con);
		
			// update local cache
			if (groups != null) {
				m_dbcon.updateGroups(groups);
			}
		}
		
		return groups;
	}
	
	public String[] getGroupDocumentIDs(MGroup group) {
		String [] docids = null;

		docids = m_dbcon.getGroupDocIDs(group);
		
		// local cache failed - get it online
		if (docids == null || docids.length == 0) {
			docids = m_webcon.getGroupDocuments(group);
		
			// update local cache
			if (docids != null) {
				
				//FIXME: find a better solution to store ids
				for(int i=0;i<docids.length;i++) {
					docids[i] = group.id + "/" + docids[i];
				}
				
				m_dbcon.updateGroupDocs(group, docids);
			}
		}
		
		return docids;
	}
	
	public MDocument fillGroupDocument(MGroup group, MDocument doc) {
		MDocument result = null;
		result = m_dbcon.getDocument(doc);
		
		if (result == null) {
			result = m_webcon.getGroupDocument(group, doc);
			if (result!= null) m_dbcon.updateDocument(result);
		}
		return result;
	}
	
	public MDocument fillDocument(MDocument doc) {
		
		MDocument result = null;
		result = m_dbcon.getDocument(doc);
		
		if (result == null) {
			result = m_webcon.getDocument(doc);
			if (result!= null) m_dbcon.updateDocument(result);
		}
		return result;
	}


	// ///////////////////////////////////////////////////////////////////////////
	// Authentication at web service
	
	public boolean setVerificationCode(String code) {
		return m_webcon.setVerificationCode(code);
	}

	public String getAuthenticationURL() throws Exception {
		return m_webcon.getAuthenticationURL();
	}


	public void clearCache() {
		m_dbcon.clear();
	}

}
